package ru.incoremedia.gwttest.application.client.component;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import ru.incoremedia.gwttest.application.client.context.ClientContext;
import ru.incoremedia.gwttest.application.client.dto.UserInfoDto;
import ru.incoremedia.gwttest.application.client.i18n.I18N;
import ru.incoremedia.gwttest.core.client.component.ajaxloaderindicator.AjaxLoaderIndicator;
import ru.incoremedia.gwttest.core.client.component.FileUploader;
import ru.incoremedia.gwttest.core.client.dto.KeyDto;
import ru.incoremedia.gwttest.core.client.util.UuidUtils;


public class ProfilePanel extends MyPopupPanel {

    private FileUploader fileUploader;

    private VerticalPanel verticalPanel;

    private Image image;

    private static ProfilePanel instance = null;

    public static void showAsync() {
//        new RunAsync().execute(new MyAsyncResultHandler() {
//            @Override
//            public void onSucess(Serializable result) {
//                if(instance == null) {
//                    instance = new ProfilePanel(I18N.constants.settings());
//                }
//
//                instance.show();
//
//                super.onSucess(result);
//            }
//        });
    }

    private ProfilePanel(String title) {
        super(title);

        verticalPanel = new VerticalPanel();
        verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        verticalPanel.setWidth("100%");

        Hyperlink submitLink = new Hyperlink(I18N.constants.upload(), "");
        
        fileUploader = new FileUploader("avatarUploadServlet", submitLink);
        fileUploader.addStyleName("whitePanel");

        final Label label = new Label(I18N.constants.avatar());
        label.addStyleName("smallLabelDark");
        getContentPanel().add(label);

        getContentPanel().add(verticalPanel);

        image = new Image();
        image.setPixelSize(128, 128);

        KeyDto keyDto = (KeyDto)ClientContext.getInstance().getAuthInfo().getUserInfoDto().getProperty(UserInfoDto.Property.AVATAR_KEY);

        if(keyDto != null) {
            loadImage(keyDto.getId());
        } else {
            image.setUrl(GWT.getModuleBaseURL() + "no_avatar.gif");
        }

        verticalPanel.add(image);
        verticalPanel.setSpacing(10);
        verticalPanel.add(fileUploader);

        fileUploader.getFormPanel().addSubmitHandler(new SubmitHandler() {
            @Override
            public void onSubmit(SubmitEvent event) {
                AjaxLoaderIndicator.showRelativeTo(label);
            }
        });

        image.addLoadHandler(new LoadHandler() {
            @Override
            public void onLoad(LoadEvent event) {
                AjaxLoaderIndicator.hide();
            }
        });

        fileUploader.getFormPanel().addSubmitCompleteHandler(new SubmitCompleteHandler() {
            @Override
            public void onSubmitComplete(SubmitCompleteEvent event) {
                String result = event.getResults();

                if(result.contains(FileUploader.UPLOAD_STATUS.OK.toString())) {
                    Long avatarId = Long.parseLong(result.split(" ")[1]);
                    loadImage(avatarId);
                } else if(result.equals(FileUploader.UPLOAD_STATUS.SIZE_LIMIT_EXCEPTION.toString())) {
                    Window.alert(I18N.constants.fileSizeLimit());
                    AjaxLoaderIndicator.hide();
                } else if(result.equals(FileUploader.UPLOAD_STATUS.SECURITY_EXCEPTION.toString())) {
                    Window.Location.reload();
                } else if(result.equals(FileUploader.UPLOAD_STATUS.FILE_FORMAT_EXCEPTION.toString())) {
                    Window.alert(I18N.constants.fileFormat());
                    AjaxLoaderIndicator.hide();
                } else if(result.equals(FileUploader.UPLOAD_STATUS.UNKNOWN_EXCEPTION.toString())) {
                    Window.alert(I18N.constants.unknownError());
                    AjaxLoaderIndicator.hide();
                }
            }
        });
    }

    private void loadImage(long id) {
        image.setUrl("resource?id=" + id + "&uuid=" + UuidUtils.uuid(6));
    }
}
